/**
 * 
 */
package cn.springsoter.core.security.utils;

import org.apache.commons.lang3.StringUtils;

import cn.springsoter.core.security.constant.SecurityConstant;
import cn.springsoter.core.tool.utils.StringPool;

/**
 * 脱敏工具类（支付宝标准）
 * 
 * 参考支付宝：https://opendocs.alipay.com/open/200/security
 * 
 * @author kevin
 *
 */
public class SensitiveInfoUtil {
	private SensitiveInfoUtil() {}

	/**
	 * [中文姓名] 保留前0后1<例子：**白>
	 */
	public static String chineseName(final String fullName, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(fullName)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(fullName, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);
	}

	/**
	 * [身份证号] 保留前1后1<例子：1***************2>
	 */
	public static String idCardNum(final String id, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(id)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(id, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);
	}

	/**
	 * [固定电话] 保留后4位，其他隐藏<例子：****1234>
	 */
	public static String fixedPhone(final String num, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(num)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(num, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);
	}

	/**
	 * [手机号码] 保留前3后2，其他隐藏<例子:138********34>
	 */
	public static String mobilePhone(final String num, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(num)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(num, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);

	}

	/**
	 * [地址] 保留前6位，其他隐藏<例子：北京市海淀区****>
	 *
	 */
	public static String address(final String address, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(address)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(address, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);
	}

	/**
	 * [电子邮箱] 邮箱前缀仅显示第一个字母，其他隐藏，@及后面的地址显示<例子:g**@163.com>
	 */
	public static String email(final String email, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(email)) {
			return StringUtils.EMPTY;
		}
		final int index = StringUtils.indexOf(email, "@");
		if (index <= 1) {
			return email;
		} else {
			String emailPrefix = StringUtils.substring(email, 0, index);
			return RuleUtil.keepLeftMAndRightN(emailPrefix, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK)
					.concat(StringUtils.mid(email, index, StringUtils.length(email)));
		}
	}

	/**
	 * [银行卡号] 保留后4位，其他隐藏<例子:***************1234>
	 */
	public static String bankCard(final String cardNum, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(cardNum)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(cardNum, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);
	}

	/**
	 * [公司开户银行联号] 保留前2位，其他隐藏<例子:12********>
	 */
	public static String cnapsCode(final String code, final int keepLeft, final int keepRight) {
		if (StringUtils.isBlank(code)) {
			return StringUtils.EMPTY;
		}
		return RuleUtil.keepLeftMAndRightN(code, keepLeft, keepRight, SecurityConstant.REPLACE_TYPE_1_1, StringPool.ASTERISK);
	}
	
}
